package com.phcx.corporate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.phcx.corporate.model.AdminInfo;
import com.phcx.corporate.model.RegOffice;
import com.phcx.corporate.model.Result;
import com.phcx.corporate.service.RegOfficeService;
import com.phcx.corporate.utils.SessionUtil;
import com.phcx.corporate.vo.RegOfficeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/*
 * @Author: zmh
 * @Date: 2023/05/23/15:03
 * @Description: 获取注册页面、修改页面级联列表（登记机关选项）的信息
 *
 */
@RestController
@RequestMapping("/reg")
public class RegOfficeController {

    @Autowired
    RegOfficeService regOfficeService;

    @RequestMapping("/getList")
    public Result<List<RegOfficeVo>> getRegOfficeList(HttpServletRequest request){

        AdminInfo adminInfo1 = SessionUtil.sessionExpiration(request);
        if (adminInfo1 == null){
            return Result.fail();
        }

        // 管理员获取级联列表逻辑
        AdminInfo admin = (AdminInfo) request.getSession().getAttribute("admin");
        String accountType = admin.getAccountType();
        try{
            if ("1".equals(accountType)) {
                // 根据根节点信息获取级联列表
                QueryWrapper<RegOffice> wrapper = new QueryWrapper<>();
                wrapper.eq("reg_id", "1");
                RegOffice root = regOfficeService.getOne(wrapper);
                String licence42Code = admin.getLicence42Code();
                List<RegOfficeVo> list = getPartList(root, licence42Code);
                return Result.ok(list);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }


        // 根据根节点信息获取级联列表
        QueryWrapper<RegOffice> wrapper = new QueryWrapper<>();
        wrapper.eq("reg_id", "1");
        RegOffice root = regOfficeService.getOne(wrapper);
        List<RegOfficeVo> list = getList(root);

        return Result.ok(list);
    }

    // 管理员的级联列表获取
    private List<RegOfficeVo> getPartList(RegOffice root, String licence42Code){
        // 菜单
        List<RegOfficeVo> VOList = new ArrayList<>();

        // 获取一个菜单项
        RegOfficeVo regOfficeVo = null;

        // 获取三级菜单内容
        LambdaQueryWrapper<RegOffice> wrapper = null;
        wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RegOffice::getRegId,root.getRegId());
        RegOffice menu1 = regOfficeService.getOne(wrapper);

        wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RegOffice::getLicence42Code, licence42Code);
        RegOffice menu3 = regOfficeService.getOne(wrapper);

        wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RegOffice::getRegId, menu3.getParentId());
        RegOffice menu2 = regOfficeService.getOne(wrapper);

        RegOfficeVo regOfficeVo1 = regOfficeTORegOfficeVo(menu1);
        RegOfficeVo regOfficeVo2 = regOfficeTORegOfficeVo(menu2);
        RegOfficeVo regOfficeVo3 = regOfficeTORegOfficeVo(menu3);

        // 组装三级菜单内容
        ArrayList<RegOfficeVo> regOfficeVos = null;

        regOfficeVos = new ArrayList<>();
        regOfficeVos.add(regOfficeVo3);
        regOfficeVo2.setChildren(regOfficeVos);

        regOfficeVos = new ArrayList<>();
        regOfficeVos.add(regOfficeVo2);
        regOfficeVo1.setChildren(regOfficeVos);

        VOList.add(regOfficeVo1);

        return VOList;
    }


    // 将数据库中登记机关转化为级联列表数据
    private List<RegOfficeVo> getList(RegOffice root){
        List<RegOfficeVo> VOList = new ArrayList<>();

        // 如果有二级节点
        if(hasNext(root)){
            // 将根节点转化为vo
            RegOfficeVo rootVo = regOfficeTORegOfficeVo(root);
            // 设置根节点的flag
            rootVo.setHasChildren(true);
            // 获取一个vo列表
            List<RegOfficeVo> rootChildrenVos = new ArrayList<>();
            // 获取二级节点
            List<RegOffice> children = getChildren(root);
            // 遍历二级节点
            for (int i = 0; i < children.size(); i++) {
                // 将二级节点转化为VO 添加至List
                RegOfficeVo regOfficeVo = regOfficeTORegOfficeVo(children.get(i));
                rootChildrenVos.add(regOfficeVo);
                // 如果有三级节点
                if(hasNext(children.get(i))){
                    regOfficeVo.setHasChildren(true);
                    ArrayList<RegOfficeVo> regOfficeVos = new ArrayList<>();
                    // 获取三级节点数据
                    List<RegOffice> children1 = getChildren(children.get(i));
                    // 遍历三级节点数据
                    for (int j = 0; j < children1.size(); j++) {
                        // 将三级节点转化为VO 添加至List
                        RegOfficeVo regOfficeVo1 = regOfficeTORegOfficeVo(children1.get(j));
                        regOfficeVos.add(regOfficeVo1);
                    }
                    regOfficeVo.setChildren(regOfficeVos);
                }
            }
            rootVo.setChildren(rootChildrenVos);
            VOList.add(rootVo);
        }
        return VOList;
    }



    // 获取子节点列表
    private List<RegOffice> getChildren(RegOffice regOffice){
        QueryWrapper<RegOffice> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",regOffice.getRegId());
        return regOfficeService.list(wrapper);
    }

    // regOffice转化为regOfficeVo
    private RegOfficeVo regOfficeTORegOfficeVo(RegOffice regOffice){
        RegOfficeVo regOfficeVo = new RegOfficeVo();
        regOfficeVo.setValue(regOffice.getLicence42Code());
        regOfficeVo.setLabel(regOffice.getLicence42());
        return regOfficeVo;
    }

    // 判断regOffice是否有子节点
    private boolean hasNext(RegOffice regOffice){
        QueryWrapper<RegOffice> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",regOffice.getRegId());
        int count = regOfficeService.count(wrapper);
        if (count > 0) {
            return true;
        }
        return false;
    }
}
